<template>
  <div class="login_container">
    <div class="login_box">
        <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 表单区域 -->
    <el-form class="formAll" :model="form" :rules="rules" ref="formLogin">
      <el-form-item prop="username">
          <el-input class="inputs" prefix-icon="iconfont icon-jurassic_user"
          v-model="form.username" placeholder="请输入账号" 
          ></el-input>
      </el-form-item>
      <el-form-item prop="password"> 
          <el-input type="password" class="inputs" prefix-icon="iconfont icon-tianchongxing-"
          v-model="form.password" placeholder="请输入密码" 
          ></el-input>
      </el-form-item>
      <el-form-item class="btns">
          <el-button type="primary" @click="loginForm">登录</el-button>
          <el-button type="info" @click='clearForm' >重置</el-button>
        </el-form-item>
    </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data(){
    return{
      form:{
        username:'admin',
        password:'123456'
      },
     rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入密码', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ], 
     }
    }
  },
  methods: {
    clearForm(){
      this.$refs.formLogin.resetFields();
    },
    loginForm(){
      
      this.$refs.formLogin.validate(async valid => {
        if(!valid) return;
          const {data} = await this.$http.post('login',this.form);
            if(data.meta.status !== 200) {
        //如果status的值不等于200时弹出错误框 
                return this.rightOpen()}
        //当status的值等于200时返回正确，可进入管理系统
            else{
              window.sessionStorage.setItem("token",data.data.token)
              
            return this.errorOpen()
            }
      })
    }, 
  //弹出正确框函数
     rightOpen() {
        this.$message({
          showClose: true,
          message: '错了哦，这是一个错误账户',
          type: 'error'
        });
      },
    errorOpen(){
      this.$message({
        showClose: true,
          message: '恭喜你，登录成功',
          type: 'success',
          duration:1000
      })
      //等一秒后跳转至/home主要界面     
      setTimeout(() => {
      this.$router.push("/home")
      }, 1000);
    }, 
}
};
</script>

<style lang="less" scoped>
.login_container {
  height: 100%;
  background-color: #2b4b6b;
}
.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.avatar_box {
  z-index: 1;
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
  }
  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  
  }
  .formAll{
    position: absolute;
    left: 50%;
    top: 30%;
    transform: translate(-50%);
  }
  .inputs{
    width: 400px;
  }
</style>
